package com.labichaoka.chaoka.ui;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.labichaoka.chaoka.R;
import com.labichaoka.chaoka.base.BaseActivity;
import com.labichaoka.chaoka.ui.home.adapter.MainViewPagerAdapter;
import com.labichaoka.chaoka.ui.home.fragment.home.HomeFragment;
import com.labichaoka.chaoka.ui.home.fragment.ku.KuFragment;
import com.labichaoka.chaoka.ui.home.fragment.my.MyFragment;
import com.labichaoka.chaoka.utils.SharedPreferencesManager;
import com.labichaoka.chaoka.utils.ToastUtils;
import com.labichaoka.chaoka.widget.TopTitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class MainActivity extends BaseActivity {

    @BindView(R.id.viewPagerLayout)
    ViewPager viewPager;
    @BindView((R.id.home_icon))
    ImageView homeIcon;
    @BindView((R.id.ku_icon))
    ImageView kuIcon;
    @BindView((R.id.my_icon))
    ImageView myIcon;

    @BindView(R.id.txtHome)
    TextView txtHome;
    @BindView(R.id.txtKu)
    TextView txtKu;
    @BindView(R.id.txtMy)
    TextView txtMy;

    private List<Fragment> list;
    // 定义一个long型变量，用于判断两次点击的间隔
    private long exitTime;

    private int index=0;

    @Override
    protected int initLayoutId() {
        return R.layout.activity_main;
    }

    @Override
    protected void initView() {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }


    @Override
    protected void initData() {
        list=new ArrayList<>();
        list.add(new HomeFragment());
        list.add(new KuFragment());
        list.add(new MyFragment());

        viewPager.setAdapter(new MainViewPagerAdapter(getSupportFragmentManager(),list));

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                index=position;
                setBottomItem(position);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }


    //设置item
    public void setBottomItem(int position){
        switch (position){
            case 0:
                resetUI();
                homeIcon.setImageResource(R.mipmap.home_selected);
                break;
            case 1:
                resetUI();
                kuIcon.setImageResource(R.mipmap.monku_selected);
                break;
            case 2:
                resetUI();
                myIcon.setImageResource(R.mipmap.my_selected);
                break;
        }
    }


    @OnClick({R.id.home_layout,R.id.ku_layout,R.id.my_layout})
    public void clickB(View v){
        switch (v.getId()){
            case R.id.home_layout:
                index=0;
                viewPager.setCurrentItem(0);
                break;
            case R.id.ku_layout:
                index=1;
                viewPager.setCurrentItem(1);
                break;
            case R.id.my_layout:
                index=2;
                viewPager.setCurrentItem(2);
                break;
        }
    }

    public void resetUI(){
        homeIcon.setImageResource(R.mipmap.home_nomal);
        kuIcon.setImageResource(R.mipmap.monku_nomal);
        myIcon.setImageResource(R.mipmap.my_nomal);
    }

    public void exit() {
        if (System.currentTimeMillis() - exitTime > 2000) {
            ToastUtils.show("再按一次退出应用");
            exitTime = System.currentTimeMillis();
        } else {
            System.exit(0);
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        String flag=SharedPreferencesManager.getInstance().getString("flag","2");

        if (keyCode == KeyEvent.KEYCODE_BACK) {
            switch (index){
                case 0:
                    exit();
                    break;
                case 1:
                    if (flag.equals("1")||flag.equals("2")){
                        exit();
                    }
                    break;
                case 2:
                    exit();
                    break;
            }

            return false;
        }
        return super.onKeyDown(keyCode, event);//退出整个应用程序
    }

}
